<?php
namespace App\Logic\SalesReportLT;

use App\Models\Basics\ExchangeRate;
use App\Models\MonthlyReportLT\Payment\PaymentReFee;
use App\Models\MonthlyReportCommon\Cost\PriceBasics;
use App\Models\MonthlyReportLT\Payment\PaymentCancelFee;
use App\Models\MonthlyReportLT\Payment\PaymentThFee;
use App\Models\Product\ProductSku;
use App\Models\SalesReportLT\PaymentDetail as TPaymentDetail;
use Illuminate\Support\Facades\DB;
class PaymentCommonFee
{
    public static function exePayment($date){
        try{
            $start = time();
            $start_memory = memory_get_usage();
            $classify = [];
            $purchasePrice = [];
            $rateOdds = ExchangeRate::getRateOddsToRMB('JPY',strtotime($date));
            $data = [];
            $res1 = PaymentThFee::query()
                ->where('report_date',$date)
                ->where('status',2)
                ->get();
            foreach ($res1 as $re){
                if(!array_key_exists($re->psku_id,$data)){
                    $data[$re->psku_id] = ['thfba' => $re->total ?: 0,
                        'quantity' => 0,'total' => 0,
                        'refund_num' => 0,'refund_amount' => 0,
                    ];
                }else{
                    $data[$re->psku_id]['thfba'] = $re->total ?: 0;
                }
            }
            $res2 = PaymentCancelFee::query()
                ->where('report_date',$date)
                ->where('status',2)
                ->get();
            foreach ($res2 as $re){
                if(!array_key_exists($re->psku_id,$data)){
                    $data[$re->psku_id] = ['thfba' => 0,
                        'quantity' => $re->quantity ?: 0,'total' => $re->total ?: 0,
                        'refund_num' => 0,'refund_amount' => 0
                    ];
                }else{
                    $data[$re->psku_id]['quantity'] = $re->quantity ?: 0;
                    $data[$re->psku_id]['total'] = $re->total ?: 0;
                }
            }
            $res3 = PaymentReFee::query()
                ->where('report_date',$date)
                ->where('status',2)
                ->get();
            foreach ($res3 as $re){
                if(!array_key_exists($re->psku_id,$data)){
                    $data[$re->psku_id] = ['thfba' => 0,
                        'quantity' => 0,'total' => 0,
                        'refund_num' => $re->quantity ?: 0,'refund_amount' => $re->sales ?: 0
                    ];
                }else{
                    $data[$re->psku_id]['refund_num'] = $re->quantity ?: 0;
                    $data[$re->psku_id]['refund_amount'] = $re->sales ?: 0;
                }
            }
            DB::beginTransaction();
            foreach($data as $pSkuId => $line){
                //品类
                if(!array_key_exists($pSkuId,$classify)){
                    $classify[$pSkuId] =  ProductSku::getValueById($pSkuId,'classify_id');
                }
                //采购价格
                $priceKey = $pSkuId.'-'.$date;
                if(!array_key_exists($priceKey,$purchasePrice)){
                    $purchasePrice[$priceKey] = PriceBasics::getPrice($pSkuId,$date);
                }
                $purchasePriceRMB = $purchasePrice[$priceKey];
                //退款费用
                $refund_amount = $line['refund_amount'];//退款费用
                $refund_amount_rmb = round($refund_amount * $rateOdds,3);//退款费用_RMB
                //额外费用
                $other_fee_rmb = round($line['quantity'] * $purchasePriceRMB  + $line['total'],3);
                $thfba = $line['thfba'];//第三方FBA发货费
                $thfba_rmb = round($thfba * $rateOdds,3);//第三方FBA发货费_RMB
                //毛利值RMB
                $earning_rmb = $profit_rmb = -round($refund_amount_rmb + $other_fee_rmb + $thfba_rmb,3);//毛利值RMB，销售收入
                TPaymentDetail::query()->updateOrCreate(
                    [
                        'report_date' => $date,
                        'psku_id' => $pSkuId,
                    ],
                    [
                        'classify_id' => $classify[$pSkuId],
                        'seller_id' => 124,
                        'rate_odds' => $rateOdds,
                        'refund_num' => $line['refund_num'],
                        'refund_amount' => $refund_amount,
                        'refund_amount_rmb' => $refund_amount_rmb,
                        'other_fee_rmb' => $other_fee_rmb,
                        'thfba' => $thfba,
                        'thfba_rmb' => $thfba_rmb,
                        'profit_rmb' => $profit_rmb,
                        'earning_rmb' => $earning_rmb
                    ]
                );
            }
            DB::commit();
            $end = time();
            $end_memory = memory_get_usage();
            print_r(($end - $start).'--------'.(($end_memory - $start_memory)/1024/1024)) ;
        }catch (\Exception $e){
            report($e);
        }
    }
}
